package com.epoint.mobile.demo.drag;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;

import com.epoint.mobileoa.R;

public class ImageCell extends ImageView implements DragSource, DropTarget
{
	public boolean mEmpty = true;
	public int mCellNumber = -1;
	public GridView mGrid;
	public ImageCell (Context context) {
		super (context);
	}
	public ImageCell (Context context, AttributeSet attrs) {
		super (context, attrs);
	}
	public ImageCell (Context context, AttributeSet attrs, int style) {
		super (context, attrs, style);
	}

	public boolean allowDrag () {
		return !mEmpty;
	}

	public void setDragController (DragController dragger)
	{
	}

	public void onDropCompleted (View target, boolean success)
	{
		if(!CommonStatic.DragParent.contains("FrameLayout")){
			if (success) {
				mEmpty = true;
				if (mCellNumber >= 0) {
					int bg = mEmpty ? R.color.cell_empty : R.color.cell_filled;
					setBackgroundResource (bg);
					setImageDrawable (null);
				} else {
					// For convenience, we use a free-standing ImageCell to
					// take the image added when the Add Image button is clicked.
					setImageResource (0);
				}
			}
		}
	}

	public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
			DragView dragView, Object dragInfo)
	{
		// Mark the cell so it is no longer empty.
		mEmpty = false;
		int bg = mEmpty ? R.color.cell_empty : R.color.cell_filled;
		setBackgroundResource (R.color.transparent);
		ImageView sourceView = (ImageView) source;
		Drawable d = sourceView.getDrawable ();
		if (d != null) {
			this.setImageDrawable (d);
		}
	}

	/**
	 * React to a dragged object entering the area of this DropSpot.
	 * Provide the user with some visual feedback.
	 */    
	public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
			DragView dragView, Object dragInfo)
	{
		int bg = mEmpty ? R.color.cell_empty_hover : R.color.cell_filled_hover;
		setBackgroundResource (bg);
	}

	/**
	 * React to something being dragged over the drop target.
	 */    
	public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
			DragView dragView, Object dragInfo)
	{
	}

	/**
	 * React to a drag 
	 */    
	public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
			DragView dragView, Object dragInfo)
	{
		int bg = mEmpty ? R.color.cell_empty : R.color.cell_filled;
		setBackgroundResource (bg);
	}

	/**
	 * Check if a drop action can occur at, or near, the requested location.
	 * This may be called repeatedly during a drag, so any calls should return
	 * quickly.
	 * 
	 * @param source DragSource where the drag started
	 * @param x X coordinate of the drop location
	 * @param y Y coordinate of the drop location
	 * @param xOffset Horizontal offset with the object being dragged where the
	 *            original touch happened
	 * @param yOffset Vertical offset with the object being dragged where the
	 *            original touch happened
	 * @param dragView The DragView that's being dragged around on screen.
	 * @param dragInfo Data associated with the object being dragged
	 * @return True if the drop will be accepted, false otherwise.
	 */
	public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
			DragView dragView, Object dragInfo)
	{
		return mEmpty  && (mCellNumber >= 0);
	}

	public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
			DragView dragView, Object dragInfo, Rect recycle)
	{
		return null;
	}
} 
